﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
namespace SPCFMedicineInformer.WebService
{
    /// <summary>
    /// Summary description for ConsultationSymptoms
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class ConsultationSymptoms : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetSymptoms(QueryString variables)
        {
            SymptomListing symptomContainer = null;
            IEnumerable<ReturnValue> datas;
            string strExceptions = !string.IsNullOrEmpty(variables.Exceptions) ? variables.Exceptions : ""; 
            List<string> exceptions = variables.Exceptions.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries).ToList();

            if (variables.CategoryId > 0)
            {
                symptomContainer = NodeLocator.GetNodesOfExactType<SymptomListing>(variables.CategoryId).FirstOrDefault();
            }
            if (symptomContainer != null)
            {
                datas = NodeLocator.GetNodesOfExactType<Symptom>(symptomContainer.Id)
                        .Select(r => new ReturnValue { Id = r.Id, Name = r.Name, Description = r.SymptomDescription });
            }
            else {
                datas = NodeLocator.GetNodesOfExactType<Symptom>()
                        .Select(r => new ReturnValue { Id = r.Id, Name = r.Name, Description = r.SymptomDescription });
            }

            var results = datas.Where(node =>
                                node.Name.ToLower().Contains(variables.Query.ToLower()) &&
                                !exceptions.Contains(node.Id.ToString()))
                            .OrderBy(node => node.Name)
                            .ToList();                            

            return JsonConvert.SerializeObject(results);
        }
        [WebMethod]
        public string GetSymptomsByIds(SymptomsVariables variables)
        {
            var list = variables.StrIds.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries).ToList();
            var result = NodeLocator.GetNodesOfExactType<Symptom>().Where(node => list.Contains(node.Id.ToString()))
                                                                   .OrderBy(node => node.Name)
                                                                   .Select(node => new ReturnValue { Id = node.Id, Name = node.Name, Description = node.SymptomDescription })
                                                                   .ToList();

            return JsonConvert.SerializeObject(result);
        }
        [WebMethod]
        public string GetMedicines(QueryString variables)
        {
            IEnumerable<ReturnValue> datas;
            string strExceptions = !string.IsNullOrEmpty(variables.Exceptions) ? variables.Exceptions : "";
            List<string> exceptions = variables.Exceptions.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (variables.CategoryId > 0)
            {
                datas = NodeLocator.GetNodesOfExactType<MedicineItem>().Where(node => node.Parent().ParentId == variables.CategoryId)
                                                                       .Select(node => new ReturnValue { Id = node.Id, Name = node.Name });
            }
            else
            {
                datas = NodeLocator.GetNodesOfExactType<MedicineItem>().Select(node => new ReturnValue { Id = node.Id, Name = node.Name});
            }

            var result = datas.Where(node => node.Name.ToLower().Contains(variables.Query.ToLower()) && 
                                    !exceptions.Contains(node.Id.ToString()))
                                .OrderBy(node => node.Name)
                                .ToList();
            return JsonConvert.SerializeObject(result);
        }
        [WebMethod]
        public string GetMedicinesByIds(MedicinesVariables variables)
        {
            var list = variables.StrIds.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var result = NodeLocator.GetNodesOfExactType<MedicineItem>().Where(node => list.Contains(node.Id.ToString()))
                                                                                       .OrderBy(node=>node.Name)
                                                                                       .Select(node => new ReturnValue { Id = node.Id, Name = node.Name})
                                                                                       .ToList();
            return JsonConvert.SerializeObject(result);
        }

        public class MedicinesVariables
        {
            public string StrIds { get; set; }
        }
        public class SymptomsVariables
        {
            public string StrIds { get; set; }
        }
        public class QueryString
        {
            public int CategoryId { get; set; }
            public string Query { get; set; }
            public string Exceptions { get; set; }
        }

        public class ReturnValue
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}
